<template>
  <div class="my-taskhistory">
    <eagle-tab-page-list
      url="/DangerCheckTaskJG/GetPageData"
      :params="params"
      @initActive="_initActive"
      @initList="_initList"
      ref="eagleTabPageList"
    >
      <template v-if="active === 0">
        <eagle-cell class="card" v-for="(item, index) in list" :key="index">
          <eagle-tag
            v-if="item.Status == 30 || item.Status == 100"
            class="info_tag"
            type="normal"
            >已提交/待验收
          </eagle-tag>
          <div class="warn_item">
            <div @click="goDetail(item)">

              <eagle-row gutter="20" class="dep_sort">
                <eagle-col span="24">
                  {{ index + 1 }}、{{ item.CheckTaskName }}</eagle-col
                >
              </eagle-row>

               <eagle-row gutter="20" class="dep_sort">
                <eagle-col span="24">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_enterprise.png"
                    alt=""
                  />企业名称：{{ item.EnterpriseName }}
                </eagle-col>
              </eagle-row>

              <eagle-row gutter="24" class="dep_sort">
                <eagle-col span="20">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_type.png"
                    alt=""
                  />隐患类型：{{ item.CheckTypeName }}</eagle-col
                >
                <eagle-col span="10">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_danger.png"
                    alt=""
                  />隐患/检查：{{ item.HiddenDangerCnt }}/{{
                    item.CheckCnt
                  }}</eagle-col
                >
              </eagle-row>

              <eagle-row class="dep_sort">
                <eagle-col span="13">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_user.png"
                    alt=""
                  />创建人：{{ item.CreateChnName }}</eagle-col
                >
              </eagle-row>

             <eagle-row gutter="20" class="dep_sort">
                <eagle-col span="24">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_date.png"
                    alt=""
                  />{{ item.StartDate | formatDate }}至{{
                    item.EndDate | formatDate
                  }}</eagle-col
                >
              </eagle-row>
          </div>
          <div

                class="dep_btm"
                 v-if="item.Status == 20"
              >
                <div class="btm_item" @click="handleAdd(item)">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_edit.png"
                    alt=""
                  />
                  <div class="mr-10">提交检查任务</div>
                </div>
              </div>
            </div>
        </eagle-cell>
      </template>
      <template v-if="active === 1">
        <eagle-checkgroup
          ref="checkboxGroup"
          @change="handleChange"
          v-model="result"
        >
          <eagle-cell
            class="card card_check"
            v-for="(item, index) in list"
            :key="index"
          >
            <div class="warn_item">
              <div @click="goToDetail(item)">

                            <eagle-row gutter="20" class="dep_sort">
                <eagle-col span="24">
                  {{ index + 1 }}、{{ item.CheckTaskName }}</eagle-col
                >
              </eagle-row>

               <eagle-row gutter="20" class="dep_sort">
                <eagle-col span="24">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_enterprise.png"
                    alt=""
                  />企业名称：{{ item.EnterpriseName }}
                </eagle-col>
              </eagle-row>

              
              <eagle-row gutter="24" class="dep_sort">
                <eagle-col span="20">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_type.png"
                    alt=""
                  />隐患类型：{{ item.CheckTypeName }}</eagle-col
                >
                <eagle-col span="10">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_danger.png"
                    alt=""
                  />隐患/检查：{{ item.HiddenDangerCnt }}/{{
                    item.CheckCnt
                  }}</eagle-col
                >
              </eagle-row>

              <eagle-row class="dep_sort">
                <eagle-col span="13">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_user.png"
                    alt=""
                  />创建人：{{ item.CreateChnName }}</eagle-col
                >
              </eagle-row>

              <eagle-row gutter="20" class="dep_sort">
                <eagle-col span="24">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_date.png"
                    alt=""
                  />{{ item.StartDate | formatDate }}至{{
                    item.EndDate | formatDate
                  }}</eagle-col
                >
              </eagle-row>

              </div>
              <!-- <eagle-divider
                v-if="item.IsCorrect && item.CreateUserName == userName"
              /> -->
            </div>
            <!-- <template #right-icon>
              <eagle-checkbox :name="item.ID" ref="checkboxes" shape="round" />
            </template> -->
          </eagle-cell>
        </eagle-checkgroup>
      </template>
      
    </eagle-tab-page-list>
  </div>
</template>

<script>
import { Dialog } from "vant";
import store from "@/store/index";
import { mapGetters } from "vuex";
import { getDateTime } from "@/utils/getDate";

export default {
  data() {
    return {
      active: 0,
      value: "",
      list: [],
      count: 0,
      backCount: 0,
      userlist: [],
      backUserList: [],
      result: [],
      backResult: [],
      OriginType: "",
      params: [
        {
          title: "待提交的任务",
          dataType: "NotSubmit",
          ctCode: this.$route.query.ctCode || "",
          badge: true,
        },
        {
          title: "已提交的任务",
          dataType: "Submited",
          ctCode: this.$route.query.ctCode || "",
          badge: false,
        },
        // {
        //   title: "未提交的任务",
        //   dataType: "NotSubmit",
        //   ctCode: this.$route.query.ctCode || "",
        //   badge: true,
        // },
      ],
    };
  },
  computed: {
    ...mapGetters(["userName"]),
  },
  created() {
    this.calcTitle();
  },
  methods: {
    _initActive(active) {
      this.active = active;
    },
    _initList(list) {
      this.list = list;
    },
    calcTitle() {
      this.ctCode = this.$route.query.ctCode;
      if (this.ctCode) {
        store.commit("updateTitle", "任务检查记录");
      } else {
        store.commit("updateTitle", "提交检查隐患");
      }
    },
    getDateTime(value) {
      return getDateTime(value);
    },
    checkAll() {
      this.$refs.checkboxGroup.toggleAll(true);
    },
    toggleAll() {
      this.$refs.checkboxGroup.toggleAll(false);
    },
    checkBackAll() {
      this.$refs.checkboxBackGroup.toggleAll(true);
    },
    toggleBackAll() {
      this.$refs.checkboxBackGroup.toggleAll(false);
    },
    openCheckTemplateView(item, ev) {
      this.$router.push({
        path: "/Danger/DangerTemplateCheckResult",
        query: {
          tCode: item.TCode,
          tRand: item.TRand,
          CompanyCode: this.CompanyCode,
          ctCode: this.$route.query.ctCode,
        },
      });
      ev.cancelBubble = true;
    },

    handleChange(ids) {
      this.userlist = [];
      ids.map((item) => {
        this.list.map((innerItem, innerIndex) => {
          if (innerItem.ID == item) {
            this.userlist.push({
              ID: innerItem.ID,
              IsCorrect: innerItem.IsCorrect,
              innerIndex: innerIndex + 1,
            });
          }
        });
      });
    },

    handleBackChange(ids) {
      this.backUserList = [];
      ids.map((item) => {
        this.list.map((innerItem, innerIndex) => {
          if (innerItem.ID == item) {
            this.backUserList.push({
              ID: innerItem.ID,
              IsCorrect: innerItem.IsCorrect,
              innerIndex: innerIndex + 1,
            });
          }
        });
      });
    },

    submit() {
      if (this.userlist.length) {
        for (let i = 0; i < this.userlist.length; i++) {
          if (!this.userlist[i].IsCorrect) {
            this.$toast(
              "第" +
                this.userlist[i].innerIndex +
                "项信息未填写完整，请先填写完后再选择"
            );
            return;
          }
        }
        let _this = this;
        Dialog.confirm({
          title: "询问",
          message: "你确定要提交选中的检查记录吗?",
        }).then(() => {
          let data = [];
          this.userlist.map((item, index) => {
            data.push(item.ID);
          });
          _this.$axios
            .post("/DangerCheckTaskDetailJG/SubmitRecords",data)
            .then((res) => {
              _this.$toast.success("提交成功!");
              _this.$refs.eagleTabPageList.getList();
            })
            .catch((err) => {
              console.log("err", err);
            });
        });
      }
    },
    backSubmit() {
      if (this.backUserList.length > 0) {
        for (let i = 0; i < this.backUserList.length; i++) {
          if (!this.backUserList[i].IsCorrect) {
            let index = i + 1;
            this.$toast(
              "第" +
                this.backUserList[i].innerIndex +
                "项信息未填写完整，请先填写完后再选择"
            );
            return;
          }
        }

        let _this = this;
        Dialog.confirm({
          title: "询问",
          message: "你确定要提交选中的检查记录吗?",
        })
          .then(() => {
            let data = [];
            _this.backUserList.map((item, index) => {
              data.push(item.ID);
            });
            _this.$axios
            .post("/OtherDangerCheckTaskDetailJG/SubmitRecords",data)
              .then((res) => {
                _this.$toast.success("提交成功!");
                _this.$refs.eagleTabPageList.getList();
              })
              .catch((err) => {
                console.log("err", err);
              });
          })
          .catch(() => {});
      }
    },

    handleAdd(item) {
      let _this=this;
      Dialog.confirm({
        title: "询问",
        message: "你确定要提交该检查记录吗?",
      })
        .then(() => {
          // on confirm
          _this.$axios
          .get("/DangerCheckTaskJG/SubmitDanger",{params:{ id: item.ID,subStatus:0 }})
          .then((res) => {
            _this.$toast.success("提交成功！");
            _this.$refs.eagleTabPageList.getList();
          });
        })
        .catch(() => {
          // on cancel
        })
    },

    handleBackAdd(item) {
      let _this=this;
      Dialog.confirm({
        title: "询问",
        message: "你确定要提交该检查记录吗?",
      })
        .then(() => {
          // on confirm
          _this.$axios
          .get("/OtherDangerCheckTaskDetailJG/SubmitRecord",{params:{ id: item.ID }})
          .then((res) => {
            this.$toast.success("提交成功！");
            _this.getList();
            // console.log('asdasdsad',res);
          });
        })
        .catch(() => {
          // on cancel
        });
    },

    goDetail(item) {
      this.$router.push({
        // path: "/Danger/DangerCheckDetailViewtwo",
        path: "/Danger/DangerCheckTaskEdit",
        query: { id: item.ID, type: "submited" },
      });
    },
    // 随手拍编辑
    goToDetail(item) {

      this.$router.push({
        // path: "/Danger/DangerCheckDetailViewtwo",
        path: "/Danger/DangerCheckTaskEdit",
        query: { id: item.ID },
      });
      // if (item.CreateUserName == this.userName && item.CheckResult == "N") {
      //   this.OriginType = item.OriginType;
      //   this.$router.push({
      //     path: "/Danger/DangerCheckTaskDetailEdit",
      //     query: { id: item.ID },
      //   });
      // } else {
      //   this.$router.push({
      //     path: "/Danger/DangerCheckDetailViewtwo",
      //     query: { id: item.ID },
      //   });
      // }
    },
    // 检查表检查编辑
    goBackDetail(item) {
      this.OriginType = item.OriginType;
      this.$router.push({
        path: "/Danger/DangerCheckTaskDetailEdit",
        query: { id: item.ID, type: true },
      });
    },
    getFile(equipmentCode) {
      this.$router.push({
        path: "/Equipment/safetyfile",
        query: { equipmentCode: equipmentCode },
      });
    },
    // 编辑设备信息
    edit(equipmentCode) {
      this.$router.push({
        path: "/Equipment/Edit",
        query: { equipmentCode: equipmentCode },
      });
    },
    // 设备检验
    editCheck(equipmentCode) {
      this.$router.push({
        path: "/Equipment/editcheck",
        query: { equipmentCode: equipmentCode },
      });
    },
  },
};
</script>

<style lang="less" scoped>
.van-divider {
  margin-top: 3px;
  margin-bottom: 3px;
}

.my-taskhistory {
  min-height: 100vh;
  background-color: #e8f1f8;
}

.info_search {
  padding: 10px;
  border-radius: 5px !important;
  .van-field__control {
    background-color: #ffffff !important;
  }
}

.all_dep {
  background-color: #ffffff;
}

.warn_item {
  border-radius: 5px;
  position: relative;
}
.warn_header {
  display: flex;
  justify-content: space-between;
  position: relative;
  margin-bottom: 10px;
  .warn_tag {
    position: absolute;
    right: 0;
    top: 0;
    border-radius: 26.64rem 0 0 26.64rem;
  }
}
.dep_sort {
  // display: flex;
  // flex-wrap: wrap;
  // margin-bottom: 5px;
  img {
    height: 15px;
    width: 15px;
    margin-right: 5px;
  }
}
.dep_btm {
  display: flex;
  justify-content: flex-end;
  .btm_item {
    display: flex;
    align-items: center;
    img {
      height: 15px;
      width: 15px;
      margin-right: 5px;
    }
  }
}

.choose_btm {
  display: flex;
  width: 100%;
  justify-content: space-between;
  position: fixed;
  bottom: 0;
  background-color: #fff;
  .choose_cancel {
    padding: 0 10px;
    display: flex;
    align-items: center;
    float: left;
  }
  .choose_confirm {
    float: right;
    border-radius: 0;
  }
}

.card_check {
  .info_tag {
    right: -35px;
  }
}
</style>
